
import QDTracker from './utils/QDTracker';
// SDK 初始化配置
QDTracker.init({
    appid: 'wxbd39d1f029c65682',
    kfuin: '3008648699',
});

App({
    onLaunch: function () {
        const updateManager = wx.getUpdateManager()

        updateManager.onCheckForUpdate(function (res) {
            // 请求完新版本信息的回调
            console.log(res.hasUpdate)
            if (res.hasUpdate) {
                updateManager.onUpdateReady(function () {
                    updateManager.applyUpdate()
                    /* wx.showModal({
                        title: '更新提示',
                        content: '新版本已经准备好，是否重启应用？',
                        showCancel:false,
                        success(res) {
                            if (res.confirm) {
                                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                                updateManager.applyUpdate()
                            }
                        }
                    }) */
                })

                updateManager.onUpdateFailed(function () {
                    // 新版本下载失败
                })

            }
        })


        this.getUserInfo();
        wx.getSystemInfo({
            success: (result) => {
                console.log(result);
                let menuButton = wx.getMenuButtonBoundingClientRect();
                console.log(menuButton);
                this.globalData.menuTop = menuButton.top;
                this.globalData.menuHeight = menuButton.height;

                this.globalData.statusBarHeight = result.statusBarHeight;
                const sys = result.system.toLowerCase().indexOf('ios') > -1 ? 'ios' : 'android';
                this.globalData.system = sys;
                this.globalData.navbarHeight = sys === 'ios' ? 44 : 48;

                this.globalData.navHeight = this.globalData.navbarHeight + result.statusBarHeight + 1.5 * menuButton.height
                this.globalData.screenHeight = result.screenHeight;
            },
        })
    },
    onShow: function () {
        const that = this;
        let user_token = wx.getStorageSync('token');
        if (user_token != '') {
            this.getUserInfo();
        }
    },
    tip: function () {
        wx.showModal({
            title: '售后服务请联系',
            content: '电话：400-688-7977',
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    wx.makePhoneCall({
                        phoneNumber: '400-688-7977'
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },

    getUserInfo: function (e) {
        var t = this;
        t.globalData.userInfo ? "function" == typeof e && e(t.globalData.userInfo) : wx.login({
            success: function (o) {
                var n = o.code;
                wx.getUserInfo({
                    withCredentials: !0,
                    lang: "zh_CN",
                    success: function (o) {
                        o.userInfo.code = n, t.doLogin(n, o), t.globalData.userInfo = o.userInfo, "function" == typeof e && e(t.globalData.userInfo);
                    },
                });
            }
        });
    },
    doLogin: function (e, t) {
        var o = this;
        e ? wx.request({
            url: o.apiUrl("user/login"),
            method: "POST",
            data: {
                userinfo: t,
                code: e
            },
            success: function (e) {
                wx.setStorage({
                    key: "token",
                    data: e.data.data.token
                }), wx.setStorage({
                    key: "openid",
                    data: e.data.data.openid
                });
                //增加购物车角标
                // o.setCartNum()
            }
        }) : console.log("获取用户登录态失败！" + res.errMsg);
    },
    globalData: {
        userInfo: null,
        scene: null,
        statusBarHeight: 20,
        navbarHeight: 44,
        system: 'ios',
        cartNumber: 0
    },
    apiUrl: function (e) {
        return "https://piwxtest.llmaicai.com/" + e;
        //return "http://www.lelemaicai.com/mobile/public/api/wx/" + e;
        return "http://www.ll.com/" + e;
    },
    //设置购物车角标
    setCartNum() {
        var that = this;
        var token = wx.getStorageSync("token");
        wx.request({
            url: that.apiUrl("cart/num"),
            method: "POST",
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": token
            },
            success: function (a) {
                wx.setTabBarBadge({
                    index: 2,
                    // text: a.data.data
                })
            }
        });
    },
    shwomessage: function (e) {
        var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 1e3,
            o = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : "warn";
        wx.showToast({
            title: e,
            icon: o,
            duration: t
        });
    },
    redirectTo: function (e) {
        wx.navigateTo({
            url: e
        });
    },
    switchTab: function (e) {
        wx.switchTab({
            url: e
        });
    },
    payOrder: function (e, t, o, n, credit_money, wx_money) {
        var a = this;
        var token = wx.getStorageSync("token");
        wx.request({
            url: a.apiUrl("payment/pay"),
            data: {
                id: e,
                open_id: t,
                code: "order.pay",
                credit_money: credit_money,
                wx_money: wx_money
            },
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": token
            },
            method: "POST",
            success: function (t) {
                /* if (500 != t.data.status_code) { */
                if (t.data.code == 0) {
                    wx.showToast({
                        title: t.data.data,
                        icon: 'none',
                        image: '',
                        duration: 1500,
                        mask: false,
                        success: (result) => {

                        },
                        fail: () => { },
                        complete: () => { }
                    });
                    return;
                }
                var i = t.data.data.wxpay;
                "" != i && wx.requestPayment({
                    timeStamp: i.timestamp,
                    nonceStr: i.nonce_str,
                    package: i.packages,
                    signType: "MD5",
                    paySign: i.sign,
                    success: function (t) {
                        "requestPayment:ok" == t.errMsg && wx.request({
                            url: a.apiUrl("payment/paySuccess"),
                            data: {
                                id: e,
                                form_id: n
                            },
                            method: "post",
                            header: {
                                "Content-Type": "application/json",
                                "X-ECTouch-Authorization": token
                            },
                            success: function (t) {
                                0 == t.data.data.code ? (wx.showToast({
                                    title: "支付成功",
                                    duration: 2e3
                                }), wx.redirectTo({ url: "../order/index?objectId=" + e })) : wx.showToast({
                                    title: "付款失败",
                                    image: "../../images/failure.png",
                                    duration: 2e3
                                });
                            }
                        });
                    },
                    fail: function (t) {
                        wx.showToast({
                            title: "支付失败",
                            image: "../../images/failure.png",
                            duration: 2e3
                        }), wx.redirectTo({ url: "../order/index?objectId=" + e });
                    }
                });
                /*  } else wx.showToast({
                     title: "支付失败",
                     image: "../../images/failure.png",
                     duration: 2e3
                 }); */
            }
        });
    },
    region: function () {
        var e = this, t = [], o = wx.getStorageSync("token");
        wx.request({
            url: e.apiUrl("region/list"),
            data: {
                id: 1
            },
            method: "post",
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": o
            },
            success: function (e) {
                for (var o = e.data.data, n = [], a = [], i = 0; i < o.length; i++) {
                    n = o[i].region_name;
                    var r = {
                        province_id: a = o[i].region_id,
                        city_id: 0,
                        county_id: 0,
                        region_name: n,
                        region_id: a
                    };
                    t.push(r);
                    for (var s, c = o[i].region, d = [], u = 0; u < c.length; u++) {
                        var g = {
                            province_id: a,
                            city_id: u + 1,
                            county_id: 0,
                            region_name: d = c[u].region_name,
                            region_id: s = c[u].region_id
                        };
                        t.push(g);
                        for (var l, f = [], p = c[u].region, h = 0; h < p.length; h++) {
                            var w = {
                                province_id: a,
                                city_id: u + 1,
                                county_id: h + 1,
                                region_name: f = p[h].region_name,
                                region_id: l = p[h].region_id
                            };
                            t.push(w);
                        }
                    }
                }
                wx.setStorageSync("region", t);
            }
        });
    },
    goodsCheckout: function (mt, setBrage = true) {
        var o = this, token = wx.getStorageSync("token"), n = "cart";
        if (!token) {
            wx.showModal({
                title: '提示',
                content: '请登录后继续',
                showCancel: false,
                success: function () {

                    wx.switchTab({
                        url: '../user/index',
                    })
                }

            })
            return;
        }
        var a = this;
        // console.log(t);
        var goods_id = mt.currentTarget.id
        // console.log(a)
        if (a) {
            wx.request({
                url: a.apiUrl("cart/add"),
                data: {
                    id: goods_id,
                    num: 1,
                    attr_id: JSON.stringify([])
                },
                method: "post",
                header: {
                    "Content-Type": "application/json",
                    "X-ECTouch-Authorization": token
                },
                success: function (t) {
                    // wx.showToast({
                    //   title: '添加成功',
                    //   icon: 'success',
                    //   duration: 2000
                    // });
                    if (setBrage) {
                        a.setCartNum();
                    }

                }
            });
        } else {
            wx.switchTab({
                url: '../user/index',
            })
        }
    },
    goodsCheckout_v2: function (mt, setBrage = true) {
        var o = this, token = wx.getStorageSync("token"), n = "cart";
        if (!token) {
            wx.showModal({
                title: '提示',
                content: '请登录后继续',
                showCancel: false,
                success: function () {

                    wx.switchTab({
                        url: '../user/index',
                    })
                }

            })
            return;
        }
        var a = this;
        // console.log(t);
        var goods_id = mt.currentTarget.id
        var remarks = mt.remarks
        // console.log(a)
        return new Promise((resolve, reject) => {
            wx.request({
                url: a.apiUrl("cart/add"),
                data: {
                    id: goods_id,
                    num: 1,
                    remarks: remarks,
                    attr_id: JSON.stringify([])
                },
                method: "post",
                header: {
                    "Content-Type": "application/json",
                    "X-ECTouch-Authorization": token
                },
                success: function (t) {
                    if (setBrage) {
                        a.setCartNum();
                    }
                    if (t.data.data.code != -1) {
                        wx.showToast({
                            title: "加入购物车成功",
                            icon: "none",
                            duration: 500
                        });
                    }
                    resolve(t);
                }
            });
        })
    },
    goodsCartUpdate: function (mt, amount, setBrage = true) {
        var o = this, token = wx.getStorageSync("token"), n = "cart";
        if (!token) {
            wx.showModal({
                title: '提示',
                content: '请登录后继续',
                showCancel: false,
                success: function () {

                    wx.switchTab({
                        url: '../user/index',
                    })
                }

            })
            return;
        }
        var a = this;
        // console.log(t);
        var goods_id = mt.currentTarget.id
        // console.log(a)
        if (a) {
            wx.request({
                url: a.apiUrl("cart/update"),
                data: {
                    goods_id: goods_id,
                    amount: amount,
                    id: mt.currentTarget.dataset.recid
                },
                method: "post",
                header: {
                    "Content-Type": "application/json",
                    "X-ECTouch-Authorization": token
                },
                success: function (t) {
                    // wx.showToast({
                    //   title: '添加成功',
                    //   icon: 'success',
                    //   duration: 2000
                    // });
                    if (setBrage) {
                        a.setCartNum();
                    }

                }
            });
        } else {
            wx.switchTab({
                url: '../user/index',
            })
        }
    },
    goodsCartDelete: function (mt, setBrage = true) {
        var o = this, token = wx.getStorageSync("token"), n = "cart";
        if (!token) {
            wx.showModal({
                title: '提示',
                content: '请登录后继续',
                showCancel: false,
                success: function () {

                    wx.switchTab({
                        url: '../user/index',
                    })
                }

            })
            return;
        }
        var a = this;
        // console.log(t);
        var goods_id = mt.currentTarget.id
        // console.log(a)
        if (a) {
            wx.request({
                url: a.apiUrl("cart/delete"),
                data: {
                    goods_id: goods_id,
                    id: 1
                },
                method: "post",
                header: {
                    "Content-Type": "application/json",
                    "X-ECTouch-Authorization": token
                },
                success: function (t) {
                    // wx.showToast({
                    //   title: '添加成功',
                    //   icon: 'success',
                    //   duration: 2000
                    // });
                    if (setBrage) {
                        a.setCartNum();
                    }

                }
            });
        } else {
            wx.switchTab({
                url: '../user/index',
            })
        }
    }
});